import http from '../utils/http'
import type {Article} from '@/model/Dynamics'


// 查询所有动态数据接口

export const get_heartisland_detail = ()=>{
	return http.get('/classes/Dynamics')
}






// 详情页交互完成后进行更新的接口

// 1. 更新点赞数据的接口
// 文章点赞（原子操作递增）
export const likePost = (objectId: string) => {
  return http.put(`/classes/Dynamics/${objectId}`, {
    "detail.interactions.likeCount": { 
      "__op": "Increment", 
      "amount": 1 
    }
  });
};

// 文章取消点赞（原子操作递减）
export const unlikePost = (objectId: string) => {
  return http.put(`/classes/Dynamics/${objectId}`, {
    "detail.interactions.likeCount": { 
      "__op": "Decrement", 
      "amount": 1 
    }
  });
};

// 添加点赞人到列表（同时更新计数器，直接传入文章详情）
export const addPostLikePerson = (postDetail: Article, newUser: {username: string; objectID: string}) => {
  const { objectId, detail } = postDetail;
  const currentLikes = detail.likeperson || [];
  
  // 检查用户是否已点赞
  if (!currentLikes.some(item => item.objectID === newUser.objectID)) {
    return http.put(`/classes/Dynamics/${objectId}`, {
      "detail.likeperson": {
        "__op": "Add",
        "objects": [newUser]
      }
      
    });
  }
  
  // 已点赞则返回原数据
  return Promise.resolve(postDetail);
};

// 从点赞列表移除用户（同时更新计数器，直接传入文章详情）
export const removePostLikePerson = (postDetail: Article, userId: string) => {
  const { objectId, detail } = postDetail;
  const currentLikes = detail.likeperson || [];
  
  // 检查用户是否已点赞
  const userToRemove = currentLikes.find(item => item.objectID === userId);
  if (userToRemove) {
    return http.put(`/classes/Dynamics/${objectId}`, {
      "detail.likeperson": {
        "__op": "Remove",
        "objects": [userToRemove]
      }
    
    });
  }
  
  // 未点赞则返回原数据
  return Promise.resolve(postDetail);
};
    








// 2. 更新评论区的接口


// 评论点赞（原子操作递增）
export const likeComment = (objectId: string, commentIndex: number) => {
  return http.put(`/classes/Dynamics/${objectId}`, {
    // 点赞数递增（数值型原子操作）
    [`detail.comments.${commentIndex}.likeCount`]: {
      "__op": "Increment",
      "amount": 1
    }
  });
};

// 评论取消点赞（原子操作递减）
export const unlikeComment = (objectId: string, commentIndex: number) => {
  return http.put(`/classes/Dynamics/${objectId}`, {
    // 点赞数递减（数值型原子操作）
    [`detail.comments.${commentIndex}.likeCount`]: {
      "__op": "Decrement",
      "amount": 1
    }
  });
};

// 给评论添加点赞人（使用AddUnique确保不重复）
export const addCommentLikePerson = (
  objectId: string, 
  commentIndex: number, 
  newUser: {username: string; objectID: string}
) => {
  return http.put(`/classes/Dynamics/${objectId}`, {
    // 数组型原子操作：AddUnique（自动去重）
    [`detail.comments.${commentIndex}.likeperson`]: {
      "__op": "AddUnique",
      "objects": [newUser]  // 符合规范：用objects包裹数组
    }
  });
};

// 从评论点赞列表移除用户（使用Remove操作）
export const removeCommentLikePerson = (
  objectId: string, 
  commentIndex: number, 
  userToRemove: {username: string; objectID: string}
) => {
  return http.put(`/classes/Dynamics/${objectId}`, {
    // 数组型原子操作：Remove
    [`detail.comments.${commentIndex}.likeperson`]: {
      "__op": "Remove",
      "objects": [userToRemove]  // 符合规范：用objects包裹数组
    }
  });
};


// 评论的回复点赞（原子操作递增）
export const likeCommentReply = (objectId: string, commentIndex: number, replyIndex: number) => {
  return http.put(`/classes/Dynamics/${objectId}`, {
    // 匹配评论回复点赞数字段路径
    [`detail.comments[${commentIndex}].replies[${replyIndex}].likeCount`]: {
      "__op": "Increment",
      "amount": 1
    }
  });
};

// 评论的回复取消点赞（原子操作递减）
export const unlikeCommentReply = (objectId: string, commentIndex: number, replyIndex: number) => {
  return http.put(`/classes/Dynamics/${objectId}`, {
    // 匹配评论回复点赞数字段路径
    [`detail.comments[${commentIndex}].replies[${replyIndex}].likeCount`]: {
      "__op": "Decrement",
      "amount": 1
    }
  });
};
